package com.ymatou.iapi.sellerquery.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery.parameter.GetSellerPaidSalesVolumeBean;
import com.ymatou.iapi.trading.sellerquery.parameter.resp.GetSellerPaidSalesVolumeResp;
import com.ymatou.iapi.trading.sellerquery.service.GetSellerPaidSalesVolumeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取商家成交额成交量接口, 新接口, .net没有这个接口 FreeMaker Template自动生成代码
 */
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetSellerPaidSalesVolume {
	private static GetSellerPaidSalesVolumeBean GetSellerPaidSalesVolumeBean;
	private static GetSellerPaidSalesVolumeCall GetSellerPaidSalesVolumeCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商家成交额成交量接口");
	}

	@Before
	public void caseUp() {
		GetSellerPaidSalesVolumeBean = new GetSellerPaidSalesVolumeBean();
		GetSellerPaidSalesVolumeCall = new GetSellerPaidSalesVolumeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(GetSellerPaidSalesVolumeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSellerPaidSalesVolumeResp expectResponse = new GetSellerPaidSalesVolumeResp();
		ConstructExpectedResponse(expectResponse);
		GetSellerPaidSalesVolumeResp actualResponse = GetSellerPaidSalesVolumeCall
				.getSellerSalesVolumeResp();
		DataUtil.verify(expectResponse.getSales(), actualResponse.getSales(), "验证sales");
		DataUtil.verify(expectResponse.getVolume(), actualResponse.getVolume(), "验证volume");
	}

	public void ConstructExpectedResponse(GetSellerPaidSalesVolumeResp response) {
		List<Map> list = execute(GetSellerPaidSalesVolumeBean.getSellerId(),
				GetSellerPaidSalesVolumeBean.getBeginTime(),
				GetSellerPaidSalesVolumeBean.getEndTime());
		if(false == DataUtil.isNullOrEmpty(list)){
			DataUtil.mapToBean(response, list.get(0));
		}
	}

	public List<Map> execute(int sellerId, String beginTime, String endTime) {
		return new DefaultWapper().executeSql(getSellerPaidSalesVolumeSql(
				sellerId, beginTime, endTime));
	}

	public static String getSellerPaidSalesVolumeSql(int sellerId,
			String beginTime, String endTime) {
		return " SELECT COUNT(o.iOrderId) AS 'volume',"
				+ " SUM(o.fTotalPrice + o.fFreight + o.fOrderDiscount + o.fDiscount - isnull(o.fUseGiftAmount, 0)"
				+ "     - isnull(o.[fSellerPromotionAmount], 0)- isnull(o.fSellerCouponAmount, 0)) AS 'sales'"
				+ " FROM dbo.Ymt_Orders o WITH ( NOLOCK )"
				+ " JOIN dbo.Ymt_OrderState s WITH ( NOLOCK )"
				+ " ON o.iorderid = s.iorderid"
				+ " WHERE o.iBuyerId = "
				+ sellerId
				+ " AND o.dPaidTime BETWEEN '"
				+ beginTime
				+ "' AND '"
				+ endTime
				+ "' AND o.iTradingStatus IN (2,3,4,16,17) AND s.fPaidAmountOfCash > 0";
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_001() {
		Logger.start(true, "获取商家成交额成交量");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			GetSellerPaidSalesVolumeBean.setSellerId(sellerId);
			GetSellerPaidSalesVolumeBean.setBeginTime(YMTDateUtil
					.getBeforeOrNextDay(-5).replace(" ", "T"));
			GetSellerPaidSalesVolumeBean.setEndTime(YMTDateUtil
					.getBeforeOrNextDay(2).replace(" ", "T"));
			GetSellerPaidSalesVolumeCall.setData(GetSellerPaidSalesVolumeBean);
			GetSellerPaidSalesVolumeCall.callService();
			DataUtil.verify(200, GetSellerPaidSalesVolumeCall.getCode(),
					"验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_002() {
		Logger.start(true, "商家id不存在");
		try {

			GetSellerPaidSalesVolumeBean.setSellerId(404);
			GetSellerPaidSalesVolumeBean.setBeginTime(YMTDateUtil
					.getBeforeOrNextDay(-5).replace(" ", "T"));
			GetSellerPaidSalesVolumeBean.setEndTime(YMTDateUtil
					.getBeforeOrNextDay(2).replace(" ", "T"));
			GetSellerPaidSalesVolumeCall.setData(GetSellerPaidSalesVolumeBean);
			GetSellerPaidSalesVolumeCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					GetSellerPaidSalesVolumeCall.getString("code"), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_003() {
		Logger.start(true, "商家id为null");
		try {
			GetSellerPaidSalesVolumeBean.setBeginTime(YMTDateUtil
					.getBeforeOrNextDay(-5).replace(" ", "T"));
			GetSellerPaidSalesVolumeBean.setEndTime(YMTDateUtil
					.getBeforeOrNextDay(2).replace(" ", "T"));
			GetSellerPaidSalesVolumeCall.setData(GetSellerPaidSalesVolumeBean);
			GetSellerPaidSalesVolumeCall.callService();
			Logger.verifyEquals(ECode.ERROR400,
					GetSellerPaidSalesVolumeCall.getString("code"), "验证Code");
			Logger.verifyEquals("100",
					GetSellerPaidSalesVolumeCall.getString("bcode"), "验证Code");
			Logger.verifyEquals("sellerId:must be greater than or equal to 1",
					GetSellerPaidSalesVolumeCall.getString("msg"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_004() {
		Logger.start(true, "beginTime = null, 获取商家成交额成交量失败");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			GetSellerPaidSalesVolumeBean.setSellerId(sellerId);
			GetSellerPaidSalesVolumeBean.setEndTime(YMTDateUtil
					.getBeforeOrNextDay(2).replace(" ", "T"));
			GetSellerPaidSalesVolumeCall.setData(GetSellerPaidSalesVolumeBean);
			GetSellerPaidSalesVolumeCall.callService();
			DataUtil.verify(ECode.ERROR400, GetSellerPaidSalesVolumeCall.getCode(),
					"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_005() {
		Logger.start(true, "endTime = null, 获取商家成交额成交量失败");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			GetSellerPaidSalesVolumeBean.setSellerId(sellerId);
			GetSellerPaidSalesVolumeBean.setBeginTime(YMTDateUtil
					.getBeforeOrNextDay(-5).replace(" ", "T"));
			GetSellerPaidSalesVolumeCall.setData(GetSellerPaidSalesVolumeBean);
			GetSellerPaidSalesVolumeCall.callService();
			DataUtil.verify(ECode.ERROR400, GetSellerPaidSalesVolumeCall.getCode(),
					"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}